/*
 * @Author: your name
 * @Date: 2021-05-10 10:08:38
 * @LastEditTime: 2021-05-12 13:05:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \after-end\app\utils\filterRoutes.js
 */
const { unique } = require('../utils/deduplicationRecursion');

function filterIpId(data, ipId) {
    let arr = [];

    data.forEach(item => {
        item.ipId.split('').forEach(val => {
            if (val === ipId.toString()) {
                arr.push(item);
            }
        });
    });

    return arr;
}

function filterRoutes(data) {
    let arr = [];
    let arr1 = [];
    let arr2 = [];
    let arr3 = [];
    let newArr = [];

    data.forEach(item => {
        if (item.key.length === 1) arr1.push(item);
        if (item.key.length > 1 && item.key.length <= 3) arr2.push(item);
        if (item.key.length > 3) arr3.push(item);
    });

    arr2.forEach(item => {
        let children = [];
        arr3.forEach(items => {
            if (items.key[0] === item.key[0] && items.key[2] === item.key[2]) {
                children.push({ title: items.name, key: items.key, path: items.path });
                newArr.push({ title: item.name, key: item.key, path: item.path, children });
            }
        });
    });

    arr1.forEach(item => {
        let children = [];
        unique(newArr).forEach(items => {
            if (items.key[0] === item.key[0]) {
                children.push({ title: items.title, key: items.key, path: items.path, children: items.children });
                arr.push({ title: item.name, key: item.key, path: item.path, children });
            }
        });
    });

    return unique(arr);
}

function filterRouter(arr) {
    const a = (arr)=>{
        return arr.map(item=>{
            if(item.children && item.children.length > 0){
                return [item.path,a(item.children)]
            }else{
                return item.path;
            }
        })
    } 
    return [...new Set(a(arr))].join().split(',');
}

module.exports = { filterIpId, filterRoutes,filterRouter }